/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/LineShape3DImpl.h"
#include "alkes/core/Memory.h"

namespace alkes {

LineShape3DImpl* LineShape3DImpl::create(IGraphics3DDevice* device, uint32_t num)
{
    AL_PRE_COND(device && num, 0);

    return new LineShape3DImpl(device, num);
}

void LineShape3DImpl::destroy()
{
    delete this;
}

LineShape3DImpl::LineShape3DImpl(IGraphics3DDevice* device, uint32_t num)
: LineShape(num)
, device_(device)
, vbuffer_(0)
{
}

LineShape3DImpl::~LineShape3DImpl()
{
}

IVertexBuffer* LineShape3DImpl::getVertexBuffer()
{
    if (vbuffer_)
        return vbuffer_;

    vbuffer_ = device_->createVertexBuffer(AL_VERTEX_XY|AL_VERTEX_COLOR);

    return vbuffer_;
}

bool LineShape3DImpl::renderTo(IGraphics3DDevice* device, uint32_t start, uint32_t count)
{
    if (0 == count)
        return true;

    AL_PRE_COND(start < getArrayNum() && construct(device, getVertexBuffer()) && vbuffer_, false);

    if (getArrayNum() < start + count)
        count = getArrayNum() - start;

    return device->drawPrimitive(vbuffer_, AL_PRIM_LINES, start * 2, count * 2);
}

bool LineShape3DImpl::construct(IGraphics3DDevice* /*device*/, IVertexBuffer* vbuffer)
{
    if (!isDirty())
        return true;

    AL_PRE_COND(vbuffer, false);

    uint32_t i;
    for (i = 0; i < getArrayNum(); ++i)
    {
        setVertex(vbuffer, i, getLine(i));
    }

    setDirty(false);

    return true;
}

void LineShape3DImpl::setVertex(IVertexBuffer* vbuffer, uint32_t index, const Line& line)
{
    Vector2D vec[2] = {
        {line.pt0.x, line.pt0.y},
        {line.pt1.x, line.pt1.y},
    };

    vbuffer->setVertex2D(index * 2, 2, vec);

    Color colors[2] = {
        line.color, line.color
    };

    vbuffer->setDiffuse(index * 2, 2, colors);
}

}
